package com.example.timeseletor.test

import android.app.Dialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.example.timeseletor.R
import com.example.timeseletor.databinding.ActivityMainBinding
import com.example.timeseletor.databinding.ActivityTestBinding
import com.manage.company.kotlin.DateSelector
import java.util.*

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        val binding= ActivityTestBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.btnText.setOnClickListener {
            showSendTimeDialog()
        }



    }


    private var bottomOrderSendTimeDialog: Dialog? = null

    private fun showSendTimeDialog() {
        bottomOrderSendTimeDialog = Dialog(this, R.style.BottomDialog)
        val contentView: View =
            LayoutInflater.from(this).inflate(R.layout.dialog_order_send_time, null)
        val calendar = Calendar.getInstance()
        val npYear: DateSelector = contentView.findViewById(R.id.yearSelector)
        val npMonth: DateSelector = contentView.findViewById(R.id.monthSelector)
        val npDay: DateSelector = contentView.findViewById(R.id.daySelector)
        var isNowYear=true//是否是本年
//        npYear.setNowNum(calendar.get(Calendar.YEAR))
//        npMonth.setNowNum(calendar.get(Calendar.MONTH)+1)
//        npDay.setNowNum(calendar.get(Calendar.MONTH)+1)
        npMonth.setNowNum(calendar.get(Calendar.MONTH)+1)
        npDay.setNowNum(calendar.get(Calendar.DAY_OF_MONTH))
        npMonth.setDefaultAttribute("月",true,13,calendar.get(Calendar.MONTH)+1)
        npDay.setDefaultAttribute("日",true,getDays(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1),calendar.get(Calendar.MONTH)+1)
        npYear.setOnSelectNumListener(object : DateSelector.OnSelectNumListener{
            override fun onSelected(num: Int) {
                if (num==calendar.get(Calendar.YEAR)){
                    isNowYear=true
                    npMonth.setDefaultAttribute("月",true,13,calendar.get(Calendar.MONTH)+1)
                }else{
                    isNowYear=false
                    npMonth.setDefaultAttribute("月",true,13,1)
                }
            }

        })

        npMonth.setOnSelectNumListener(object : DateSelector.OnSelectNumListener{
            override fun onSelected(num: Int) {
                if (num==calendar.get(Calendar.MONTH)+1&&isNowYear){
                    Log.e("sss","sss")
                    npDay.setDefaultAttribute("日",true,getDays(calendar.get(Calendar.YEAR),calendar.get(
                        Calendar.MONTH)),calendar.get(Calendar.DAY_OF_MONTH))
                }
            }

        })

        bottomOrderSendTimeDialog!!.setContentView(contentView)
        val layoutParams = contentView.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels
        contentView.layoutParams = layoutParams
        bottomOrderSendTimeDialog!!.window!!.setGravity(Gravity.BOTTOM)
        bottomOrderSendTimeDialog!!.setCancelable(true)
        bottomOrderSendTimeDialog!!.setCanceledOnTouchOutside(true)
        bottomOrderSendTimeDialog!!.window!!.setWindowAnimations(R.style.BottomDialog_Animation)
        bottomOrderSendTimeDialog!!.show()
    }

    //获取某年某月的天数
    fun getDays(year:Int,month:Int): Int {
        return Date(year,month,0).date
    }

}